package game;

import java.util.List;
import java.util.Random;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import sound.SoundManager;
import util.Vector2D;

import entities.Enemy;
import entities.SinusEnemy;

public class BossWave extends EnemyWave {

	private static Random random = new Random();
	private Enemy boss;
	private int spawnTime;
	private int timer;
	
	public BossWave(Enemy boss, List<Enemy> enemies, int minDelay, int addDelay) {
		super(enemies);
		this.boss = boss;
		spawnTime = minDelay + random.nextInt(addDelay);
		timer = spawnTime;
	}

	@Override
	public boolean isDefeated() {
		if (!boss.isAlive()) {
			return true;
		}
		return false;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		if (!level.isActive(boss)) {
			SoundManager.getSoundManager().playMusic(SoundManager.bossTheme);
			boss.spawn(level, new Vector2D(gc.getWidth() - boss.getImage().getWidth(),
								random.nextInt(gc.getHeight() - boss.getImage().getHeight())));
		}
		timer += delta;
		if (timer > spawnTime) {
				try {
					Enemy e = new SinusEnemy();
					Vector2D pos = new Vector2D(gc.getWidth() - e.getImage().getWidth(),
							random.nextInt(gc.getHeight() - e.getImage().getHeight()));
					e.spawn(level, pos);
					System.out.println("Enemy spawned!");
				} catch (IndexOutOfBoundsException e) {
					e.printStackTrace();
				} catch (SlickException e) {
					e.printStackTrace();
				}
			timer = 0;
		}
		
	}

	@Override
	public void reset() {
		for (Enemy e : enemies) {
			e.reset();
		}
		boss.reset();
	}

}
